<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {make_tree} function plugin
 *
 * Type:     function<br>
 * Name:     make_tree<br>
 * Input:<br>
 *           - values        (required) - array
 *           - title         (optional) - strint, title variable in array, default: "title"
 *           - branchname    (optional) - string, branch variable in array, default: "branch"
 *           - link          (optional) - string default: PHP_SELF
 *           - linkparam     (optional) - string, linkparam variable in array, default: "id"
 *           - linkparamname (optional) - string default: "id"
 *           - extra         (optional) - extra params added to link
 *           - rendertype    (optional) - Type for rendering tree structure, default: 'blank', allowed: 'blank, ul, ol'
 *           - levelextra    (required for rendertype) - coma separated list of extra (classes) for levels
 *           - liextra       (optional) - coma separated list of extra for LI entries
 * Purpose:  Prints the list of <option> tags generated from
 *           the passed parameters
 * @author Novachok Alexandr <novachok at ukr dot net>
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */

function smarty_function_make_tree($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');

    $values = null;
    $title = null;
    $branchname = null;
    $link = null;
    $linkparam = null;
    $linkparamname = null;
    $extra = '';
    $rendertypes = array('blank', 'ul', 'ol');
    $rendertype = 'blank';
    $levelextra = null;
    $liextra = null;

    foreach($params as $_key => $_val)
    {
        switch($_key)
        {
            case 'values':
                $$_key = (array)$_val;
                break;

            case 'title':
                $$_key = (string)$_val;
                break;

            case 'branchname':
                $$_key = (string)$_val;
                break;

            case 'link':
                $$_key = (string)$_val;
                break;

            case 'linkparam':
                $$_key = (string)$_val;
                break;

            case 'linkparamname':
                $$_key = (string)$_val;
                break;

            case 'rendertype':
                $$_key = (string)$_val;
                if(!in_array($rendertype, $rendertypes))
                {
                    $rendertype = 'blank';
                }
                break;
            case 'levelextra':
                $$_key = (string)$_val;
                $levelextra = explode(',', $levelextra);
                break;

            case 'liextra':
                $$_key = (string)$_val;
                $liextra = explode(',', $liextra);
                break;

            case 'extra':
                $$_key = $_val;
                $tmp = explode(',', $extra);
                if(count($tmp) > 1)
                {
                    $extra = $tmp;
                    unset($tmp);
                }
                break;
        }
    }

    if($title == false) $title = "title";
    $GLOBALS['title'] = $title;
    if($branchname == false) $branchname = "branch";
    $GLOBALS['branch'] = $branchname;
    if($link == false) $link = $_SERVER['PHP_SELF'];
    $GLOBALS['link'] = $link;
    if($linkparam == false) $linkparam = "id";
    $GLOBALS['linkparam'] = $linkparam;
    if($linkparamname == false) $linkparamname = "id";
    $GLOBALS['rendertype'] = $rendertype;
    $GLOBALS['levelextra'] = $levelextra;
    $GLOBALS['linkparamname'] = $linkparamname;
    $GLOBALS['liextra'] = $liextra;
    $GLOBALS['extra'] = $extra;

    $tree = render_tree($values, 0);

    return $tree;
}

function render_tree($tree_arr, $level)
{
    $begin_list  = '';
    $begin_entry = '';
    $end_list    = '';
    $end_entry   = '';
    $filler      = '';
    $str         = '';

    if ($GLOBALS['rendertype'] == 'ol')
    {
        $begin_list  = "<ol " . @$GLOBALS['levelextra'][$level] . ">\n";
        $begin_entry = "<li " . @$GLOBALS['liextra'][$level] . ">";
        $end_list    = "</ol>\n";
        $end_entry   = "</li>\n";
    } elseif ($GLOBALS['rendertype'] == 'ul')
    {
        $begin_list  = "<ul " . @$GLOBALS['levelextra'][$level] . ">\n";
        $begin_entry = "<li " . @$GLOBALS['liextra'][$level] . ">";
        $end_list    = "</ul>\n";
        $end_entry   = "</li>\n";
    }else
    {
        $filler = depth($level);
        $end_entry   = "<br>\n";
    }

    if(is_array($GLOBALS['extra']))
    {
        $link_extra = @$GLOBALS['extra'][$level];
    } else
    {
        $link_extra = $GLOBALS['extra'];
    }

    $str .= $begin_list;

    for($i=0, $t=count($tree_arr); $i<$t; $i++)
    {
        $str .= $begin_entry . $filler;
        $str .= "<a href='" . $GLOBALS['link'] . "?" . $GLOBALS['linkparamname'] . "=";
        $str .= $tree_arr[$i][$GLOBALS['linkparam']] . "' " . $link_extra . ">";
        $str .= $tree_arr[$i][$GLOBALS['title']];
        $str .= "</a>" . $end_entry;

        if(is_array($tree_arr[$i][$GLOBALS['branch']]))
        {
            $str .= render_tree($tree_arr[$i][$GLOBALS['branch']], $level+1);
        }
    }
    $str .= $end_list;

    return $str;
}

function depth($depth)
{
    $var = '';
    for ($i=1; $i<=$depth; ++$i)
    {
        $var = $var . "&nbsp;&nbsp;&nbsp;";
    }
    return $var;
}
?>